package com.websai.servlets;

import com.sai.clases.Calificacion;
import com.sai.clases.Profesor;
import com.sai.transacciones.TAlumno;
import com.sai.transacciones.TCalificacion;
import com.sai.transacciones.TProfesor;
import com.sai.utils.Utilities;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Profesores extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            int metodo = Integer.parseInt(request.getParameter("metodo"));
            switch(metodo){
                case 0:
                    Registrar(request, response);
                    break;
                case 1:
                    ConsultaGeneral(request, response);
                    break;
                case 2:
               //     Eliminar(request, response);
                    break;
                case 3:
                    Listar(request, response);
                    break;
                 case 4:
                    RegistrarCalificacion(request, response);
                    break;
                 case 5:
                    ConsultaAlumnos(request, response);
                    break;
                 case 6:
                     ConsultarMatriculas(request, response);
                     break;
                 case 7:
                     ConsultarCalificaciones(request, response);
                     break;
            }
        } finally {            
            
        }
    }
    
    public void Registrar(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        try {
            String nombre = request.getParameter("Nombre");
            String ap = request.getParameter("APaterno");
            String am = request.getParameter("AMaterno");
            String matricula = request.getParameter("Matricula");
            String correo = request.getParameter("Correo");
            String materia = request.getParameter("Materia");
            String grupo = request.getParameter("Grupo");
            
            if(Utilities.isReallyEmptyOrNull(nombre) && Utilities.isReallyEmptyOrNull(ap) && Utilities.isReallyEmptyOrNull(am)
                    && Utilities.isReallyEmptyOrNull(matricula) && Utilities.isReallyEmptyOrNull(correo)
                    && Utilities.isReallyEmptyOrNull(materia) && Utilities.isReallyEmptyOrNull(grupo)){
                out.print("Blanco");
            } else if(Utilities.isReallyEmptyOrNull(nombre)) {
                out.print("Nombre");
            } else if(Utilities.isReallyEmptyOrNull(ap)) {
                out.print("AP");
            } else if(Utilities.isReallyEmptyOrNull(am)) {
                out.print("AM");
            } else if(Utilities.isReallyEmptyOrNull(matricula)) {
                out.print("Matricula");
            } else if(Utilities.isReallyEmptyOrNull(correo)) {
                out.print("Correo");
            } else if(Utilities.isReallyEmptyOrNull(materia)) {
                out.print("Materia");
            } else if(Utilities.isReallyEmptyOrNull(grupo)) {
                out.print("Grupo");
            } else {
                Profesor profesor = new Profesor();
                profesor.setNombre(nombre);
                profesor.setApellidoP(ap);
                profesor.setApellidoM(am);
                profesor.setMatricula(matricula);
                profesor.setMateria(materia);
                profesor.setGrupo(grupo);
                
                String respuesta = TProfesor.Registrar(profesor);
                if(respuesta.equals("OK")){
                    out.print("OK");
                } else {
                    out.print(respuesta);
                }
            } 
        } finally {
            out.close();
        }
    }
    
    public void ConsultaGeneral(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        try {
            out.println(TProfesor.Consultar());
        } finally {
            out.close();
        }
    }
    
   // public void Eliminar(HttpServletRequest request, HttpServletResponse response) throws IOException {
     //   response.setContentType("text/html;charset=UTF-8");
       // PrintWriter out = response.getWriter();
        
        //try {
          //  String id = request.getParameter("id");
            
            //if(Utilities.isReallyEmptyOrNull(id)){
             //   out.print("Id");
            //} else {
              //  Profesor profesor = new Profesor();
                //profesor.setIdProfesor(Integer.parseInt(id));
                //TProfesor.Eliminar(id);
                //out.print("OK");
            //}
        //} finally {
          //  out.close();
        //}
    //}
    
    public void Listar(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        try {
            out.println(TProfesor.Listar());
        } finally {
            out.close();
        }
    }

    public void RegistrarCalificacion(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        try {
            String matricula = request.getParameter("Matricula");
            String rubro = request.getParameter("Rubro");
            String calificacion = request.getParameter("Calificacion");
            String materia = request.getParameter("Materia");
            
            
            if(Utilities.isReallyEmptyOrNull(matricula) && Utilities.isReallyEmptyOrNull(rubro) && Utilities.isReallyEmptyOrNull(calificacion)
                    && Utilities.isReallyEmptyOrNull(materia)){
                out.print("Blanco");
            } else if(Utilities.isReallyEmptyOrNull(matricula)) {
                out.print("Matricula");
            } else if(Utilities.isReallyEmptyOrNull(rubro)) {
                out.print("Rubro");
            } else if(Utilities.isReallyEmptyOrNull(calificacion)) {
                out.print("Calificacion");
            } else if(Utilities.isReallyEmptyOrNull(materia)) {
                out.print("Materia");
            
            } else {
                Calificacion calificaciones = new Calificacion();
                calificaciones.setMatricula(matricula);
                calificaciones.setRubro(rubro);
                calificaciones.setCalificacion(calificacion);
                calificaciones.setMateria(materia);
                
                String respuesta = TCalificacion.Registrar(calificaciones);
                if(respuesta.equals("OK")){
                    out.print("OK");
                } else {
                    out.print(respuesta);
                }
            } 
        } finally {
            out.close();
        }
    }
    
     public void ConsultaAlumnos(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        try {
            out.println(TAlumno.Consultar());
        } finally {
            out.close();
        }
    }
     
     public void ConsultarMatriculas(HttpServletRequest request, HttpServletResponse response) throws IOException {
         response.setContentType("text/html;charset=UTF-8");
         PrintWriter out = response.getWriter();
         
         try {
             out.println(TAlumno.ConsultarMats());
         } finally {
             out.close();
         }
     }
     
     public void ConsultarCalificaciones(HttpServletRequest request, HttpServletResponse response) throws IOException {
         response.setContentType("text/html;charset=UTF-8");
         PrintWriter out = response.getWriter();
         
         try {
             out.println(TAlumno.ConsultarCalifs());
         } finally {
             out.close();
         }
     }
     
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
